#ifndef ADAPTORPILE_H
#define ADAPTORPILE_H

#include "Constante.h"
#include "Entier.h"
#include "Reel.h"
#include "Operateur.h"
#include "Rationnel.h"
#include <vector>
#include <sstream>
using namespace std;
class AdaptorPile
{
    vector<Constante*> myStack;
public:
    AdaptorPile();
    string print(int n)const;
    void clear(){
        myStack.clear();
    }
    void swap(int x,int y){
        Constante*tmp = myStack[x];
        myStack[x] = myStack[y];
        myStack[y] = tmp;
    }
    void dup(){
        Constante*tmp;
        switch(myStack.back()->getType()){
        case Constante::INTEGER:
                tmp = new Entier(static_cast<Entier&>(*tmp));
        break;
        case Constante::RATIONNAL:
                tmp = new Rationnel(static_cast<Rationnel&>(*tmp));
        break;
        case Constante::FLOAT:
                tmp = new Reel(static_cast<Reel&>(*tmp));
        break;

        }
        myStack.push_back(tmp);

    }
    void sum(){
        Constante* result = new Entier(0);
        for(int i=0;i<myStack.size();i++)
                result = Addition::apply(*result,*myStack[i]);
        myStack.push_back(result);
    }
    void mean(){
        Constante* result = new Entier(0);
        for(int i=0;i<myStack.size();i++)
                result = Addition::apply(*result,*myStack[i]);
        myStack.push_back(Division::apply(Entier(myStack.size()),*result));
    }
    void drop(){
        myStack.pop_back();
    }
    void add(Constante* c){
        myStack.push_back(c);
    }
    Constante* top()const{
        if(myStack.size() == 0)
                return 0;
        return myStack.back();
    }
    Constante* sum()const;
    Constante* mean()const;
    int size()const{return myStack.size();}

};

#endif // ADAPTORPILE_H
